package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-05-22
 * Time: 22:23
 */
class Money implements Cloneable {
    public double m = 10.9;

    @Override
    protected Object clone()
            throws CloneNotSupportedException {
        return super.clone();
    }

}
public class Person implements Cloneable {
    public String name;
    public int age;
    Money money = new Money();

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    protected Object clone()  //注：谁调用了clone方法，谁就是this(Test中person1调用了clone)
            throws CloneNotSupportedException {
//        return super.clone();
        Person tmp = (Person)super.clone();
        tmp.money = (Money)this.money.clone();//通过对象(.)引用(.)克隆
         return tmp;
    }
}
